﻿var QueryData = function (url, cmd) {
    this.Data["AjaxRequest"] = new Object();
    this.Data["AjaxRequest"]["URL"] = url;
    this.Data["AjaxRequest"]["Command"] = cmd;
    this.URL = url;
    this.Command = cmd;
}
QueryData.prototype = {
    URL: "",
    Command: "",
    Data: new Object(),
    SetQueryData: function (container, alltrustTag) {
        var data = this.Data[alltrustTag];
        if (typeof (data) == "undefined") {
            data = this.Data[alltrustTag] = new Object();
        }
        $("[alltrusttag='" + alltrustTag + "']", container).each(function () {
            switch (this.tagName) {
                case "SPAN":
                    data[$(this).attr("name")] = $(this).text();
                    break;
                case "SELECT":
                    data[$(this).attr("name")] = $(this).val();
                    break;
                case "INPUT":
                    switch ($(this).attr("type")) {
                        case "text":
                        case "hidden":
                        case "password":
                            data[$(this).attr("name")] = $(this).val();
                            break;
                        case "checkbox":
                            if (this[0].checked == true) {
                                data[$(this).attr("name")] += $(this).val() + ",";
                            }
                            break;
                        case "radio":
                            if (this[0].checked == true) {
                                data[$(this).attr("name")] = $(this).val();
                            }
                            break;
                        default:

                    }
                    break;
                default:
                    data[$(this).attr("name")] = $(this).text();
                    break;
            }

        });
        this.Data[alltrustTag] = data;

    }

}



jQuery.extend({
    /** * @see 将javascript数据类型转换为json字符串 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp * @return 返回json字符串 */
    toJSON: function (object) {
        var type = typeof object;
        if ('object' == type) {
            if (Array == object.constructor) type = 'array';
            else if (RegExp == object.constructor) type = 'regexp';
            else type = 'object';
        }
        switch (type) {
            case 'undefined':
            case 'unknown':
                return;
                break;
            case 'function':
            case 'boolean':
            case 'regexp':
                return object.toString();
                break;
            case 'number':
                return isFinite(object) ? object.toString() : 'null';
                break;
            case 'string':
                return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case 'object':
                if (object === null) return 'null';
                var results = [];
                for (var property in object) {
                    var value = jQuery.toJSON(object[property]);
                    if (value !== undefined) results.push(jQuery.toJSON(property) + ':' + value);
                }
                return '{' + results.join(',') + '}';
                break;
            case 'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSON(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return '[' + results.join(',') + ']';
                break;
        }
    }
});


function checkAjaxResult(resultData) {
    debugger;
    if (resultData["ApplicationError"] == "True") {
        location.href = "/Pages/Error.aspx";
    }
    return true;
}

